{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "18bbd697-afa1-4fbc-9c1a-b2d69fb292a3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.doubanio.com/simple/\n",
      "Requirement already satisfied: netCDF4 in /storage/DevEnv/python/py39/lib/python3.9/site-packages (1.6.4)\n",
      "Requirement already satisfied: cftime in /storage/DevEnv/python/py39/lib/python3.9/site-packages (from netCDF4) (1.6.2)\n",
      "Requirement already satisfied: certifi in /storage/DevEnv/python/py39/lib/python3.9/site-packages (from netCDF4) (2023.5.7)\n",
      "Requirement already satisfied: numpy in /storage/DevEnv/python/py39/lib/python3.9/site-packages (from netCDF4) (1.24.4)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install netCDF4 -i https://pypi.doubanio.com/simple/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "15a95e50-fbc6-46b2-8e4f-e96b3d1692e0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['lat', 'lon', 'time', 'climatology_bounds', 'precip', 'valid_yr_count'])\n",
      "[1.25000e-01 3.75000e-01 6.25000e-01 ... 3.59375e+02 3.59625e+02\n",
      " 3.59875e+02]\n",
      "[1.25000e-01 3.75000e-01 6.25000e-01 ... 3.59375e+02 3.59625e+02\n",
      " 3.59875e+02]\n",
      "[cftime.DatetimeGregorian(1, 1, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 2, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 3, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 4, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 5, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 6, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 7, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 8, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 9, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 10, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 11, 1, 0, 0, 0, 0, has_year_zero=False)\n",
      " cftime.DatetimeGregorian(1, 12, 1, 0, 0, 0, 0, has_year_zero=False)]\n"
     ]
    }
   ],
   "source": [
    "import netCDF4 as nc\n",
    "\n",
    "nc_path = \"/home/geobeans/temp/precip.mon.ltm.0.25x0.25.v2020.nc\"\n",
    "\n",
    "#--读取netCDF格式文件，'w'表示可对文件进行写入操作，若不存在example.nc文件，则自动创建--#\n",
    "nf = nc.Dataset(nc_path)\n",
    "\n",
    "#--直接输出文件信息，包括维度，全局变量，变量名，但是看不到某个变量中保存的数据以及变量属性--#\n",
    "#print(nf)\n",
    "\n",
    "#--输出name变量的信息，可以看到变量的属性信息--#\n",
    "print(nf.variables.keys())\n",
    "\n",
    "#print(nf.variables['lon'])\n",
    "#print(nf['lon'])\n",
    "\n",
    "#查看一下time的数据\n",
    "print(nf.variables['lon'][:])\n",
    "\n",
    "lons = nf.variables['lon'][:].data\n",
    "print(lons)\n",
    "\n",
    "time = nc.num2date(nf.variables['time'][:],'days since 1800-1-1 00:00:00').data\n",
    "print(time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0886c03d-bdd3-45e0-803f-ec2517cd1b88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['data_type', 'format_version', 'handbook_version', 'reference_date_time', 'date_creation', 'date_update', 'platform_number', 'project_name', 'pi_name', 'station_parameters', 'cycle_number', 'direction', 'data_centre', 'dc_reference', 'data_state_indicator', 'data_mode', 'platform_type', 'float_serial_no', 'firmware_version', 'wmo_inst_type', 'juld', 'juld_qc', 'juld_location', 'latitude', 'longitude', 'position_qc', 'positioning_system', 'profile_pres_qc', 'profile_temp_qc', 'profile_psal_qc', 'vertical_sampling_scheme', 'config_mission_number', 'pres', 'pres_qc', 'pres_adjusted', 'pres_adjusted_qc', 'pres_adjusted_error', 'temp', 'temp_qc', 'temp_adjusted', 'temp_adjusted_qc', 'temp_adjusted_error', 'psal', 'psal_qc', 'psal_adjusted', 'psal_adjusted_qc', 'psal_adjusted_error', 'parameter', 'scientific_calib_equation', 'scientific_calib_coefficient', 'scientific_calib_comment', 'scientific_calib_date', 'history_institution', 'history_step', 'history_software', 'history_software_release', 'history_reference', 'history_date', 'history_action', 'history_parameter', 'history_start_pres', 'history_stop_pres', 'history_previous_value', 'history_qctest', 'crs'])\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "<class 'netCDF4._netCDF4.Variable'>\n",
      "float64 longitude(n_prof)\n",
      "    long_name: Longitude of the station, best estimate\n",
      "    standard_name: longitude\n",
      "    units: degree_east\n",
      "    _FillValue: 99999.0\n",
      "    valid_min: -180.0\n",
      "    valid_max: 180.0\n",
      "    axis: X\n",
      "    reference: WGS84\n",
      "    coordinate_reference_frame: urn:ogc:crs:EPSG::4326\n",
      "unlimited dimensions: \n",
      "current shape = (1,)\n",
      "filling on\n",
      "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "[-140.40567017]\n"
     ]
    }
   ],
   "source": [
    "import netCDF4 as nc\n",
    "nc_path = \"/home/geobeans/temp/nodc_D4901820_152.nc\"\n",
    "\n",
    "#--读取netCDF格式文件，'w'表示可对文件进行写入操作，若不存在example.nc文件，则自动创建--#\n",
    "nf = nc.Dataset(nc_path)\n",
    "\n",
    "# 输出文件中变量\n",
    "print(nf.variables.keys())\n",
    "\n",
    "# 读取变量\n",
    "lon = nf.variables[\"longitude\"]\n",
    "print(\"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\")\n",
    "print(lon)\n",
    "print(\"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\")\n",
    "print(nf.variables['longitude'][:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4d62c8f-53a8-4ea4-b63e-065c690bf8e4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py39",
   "language": "python",
   "name": "py39"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
